<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>arraymatcher.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_autocomplete_arraymatcher.js.html">arraymatcher.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Basic class for matching words in an array.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.AutoComplete.ArrayMatcher&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.iter&#39;);
<a name="line24"></a>goog.require(&#39;goog.string&#39;);
<a name="line25"></a>goog.require(&#39;goog.ui.AutoComplete&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Basic class for matching words in an array
<a name="line30"></a> * @constructor
<a name="line31"></a> * @param {Array} rows Dictionary of items to match.  Can be objects if they
<a name="line32"></a> *     have a toString method that returns the value to match against.
<a name="line33"></a> * @param {boolean} opt_noSimilar if true, do not do similarity matches for the
<a name="line34"></a> *     input token against the dictionary.
<a name="line35"></a> */
<a name="line36"></a>goog.ui.AutoComplete.ArrayMatcher = function(rows, opt_noSimilar) {
<a name="line37"></a>  this.rows_ = rows;
<a name="line38"></a>  this.useSimilar_ = !opt_noSimilar;
<a name="line39"></a>};
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>/**
<a name="line43"></a> * Function used to pass matches to the autocomplete
<a name="line44"></a> * @param {string} token Token to match.
<a name="line45"></a> * @param {number} maxMatches Max number of matches to return.
<a name="line46"></a> * @param {Function} matchHandler callback to execute after matching.
<a name="line47"></a> * @param {string} opt_fullString The full string from the input box.
<a name="line48"></a> */
<a name="line49"></a>goog.ui.AutoComplete.ArrayMatcher.prototype.requestMatchingRows =
<a name="line50"></a>    function(token, maxMatches, matchHandler, opt_fullString) {
<a name="line51"></a>
<a name="line52"></a>  var matches = this.getPrefixMatches(token, maxMatches);
<a name="line53"></a>
<a name="line54"></a>  if (matches.length == 0 &amp;&amp; this.useSimilar_) {
<a name="line55"></a>    matches = this.getSimilarRows(token, maxMatches);
<a name="line56"></a>  }
<a name="line57"></a>  matchHandler(token, matches);
<a name="line58"></a>};
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * Matches the token against the start of words in the row.
<a name="line63"></a> * @param {string} token Token to match.
<a name="line64"></a> * @param {number} maxMatches Max number of matches to return.
<a name="line65"></a> * @return {Array} Rows that match.
<a name="line66"></a> */
<a name="line67"></a>goog.ui.AutoComplete.ArrayMatcher.prototype.getPrefixMatches =
<a name="line68"></a>    function(token, maxMatches) {
<a name="line69"></a>  var matches = [];
<a name="line70"></a>
<a name="line71"></a>  if (token != &#39;&#39;) {
<a name="line72"></a>    var escapedToken = goog.string.regExpEscape(token);
<a name="line73"></a>    var matcher = new RegExp(&#39;(^|\\W+)&#39; + escapedToken, &#39;i&#39;);
<a name="line74"></a>
<a name="line75"></a>    goog.iter.some(this.rows_, function(row) {
<a name="line76"></a>      if (String(row).match(matcher)) {
<a name="line77"></a>        matches.push(row);
<a name="line78"></a>      }
<a name="line79"></a>      return matches.length &gt;= maxMatches;
<a name="line80"></a>    });
<a name="line81"></a>  }
<a name="line82"></a>  return matches;
<a name="line83"></a>};
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Matches the token against similar rows, by calculating &quot;distance&quot; between the
<a name="line88"></a> * terms.
<a name="line89"></a> * @param {string} token Token to match.
<a name="line90"></a> * @param {number} maxMatches Max number of matches to return.
<a name="line91"></a> * @return {Array} The best maxMatches rows.
<a name="line92"></a> */
<a name="line93"></a>goog.ui.AutoComplete.ArrayMatcher.prototype.getSimilarRows =
<a name="line94"></a>    function(token, maxMatches) {
<a name="line95"></a>
<a name="line96"></a>  var results = [];
<a name="line97"></a>
<a name="line98"></a>  goog.iter.forEach(this.rows_, function(row, index) {
<a name="line99"></a>    var str = token.toLowerCase();
<a name="line100"></a>    var txt = String(row).toLowerCase();
<a name="line101"></a>    var score = 0;
<a name="line102"></a>
<a name="line103"></a>    if (txt.indexOf(str) != -1) {
<a name="line104"></a>      score = parseInt((txt.indexOf(str) / 4).toString(), 10);
<a name="line105"></a>
<a name="line106"></a>    } else {
<a name="line107"></a>      var arr = str.split(&#39;&#39;);
<a name="line108"></a>
<a name="line109"></a>      var lastPos = -1;
<a name="line110"></a>      var penalty = 10;
<a name="line111"></a>
<a name="line112"></a>      for (var i = 0, c; c = arr[i]; i++) {
<a name="line113"></a>        var pos = txt.indexOf(c);
<a name="line114"></a>
<a name="line115"></a>        if (pos &gt; lastPos) {
<a name="line116"></a>          var diff = pos - lastPos - 1;
<a name="line117"></a>
<a name="line118"></a>          if (diff &gt; penalty - 5) {
<a name="line119"></a>            diff = penalty - 5;
<a name="line120"></a>          }
<a name="line121"></a>
<a name="line122"></a>          score += diff;
<a name="line123"></a>
<a name="line124"></a>          lastPos = pos;
<a name="line125"></a>        } else {
<a name="line126"></a>          score += penalty;
<a name="line127"></a>          penalty += 5;
<a name="line128"></a>        }
<a name="line129"></a>      }
<a name="line130"></a>    }
<a name="line131"></a>
<a name="line132"></a>    if (score &lt; str.length * 6) {
<a name="line133"></a>      results.push({
<a name="line134"></a>        str: row,
<a name="line135"></a>        score: score,
<a name="line136"></a>        index: index
<a name="line137"></a>      });
<a name="line138"></a>    }
<a name="line139"></a>  });
<a name="line140"></a>
<a name="line141"></a>  results.sort(function(a, b) {
<a name="line142"></a>    var diff = a.score - b.score;
<a name="line143"></a>    if (diff != 0) {
<a name="line144"></a>      return diff;
<a name="line145"></a>    }
<a name="line146"></a>    return a.index - b.index;
<a name="line147"></a>  });
<a name="line148"></a>
<a name="line149"></a>  var matches = [];
<a name="line150"></a>  for (var i = 0; i &lt; maxMatches &amp;&amp; i &lt; results.length; i++) {
<a name="line151"></a>    matches.push(results[i].str);
<a name="line152"></a>  }
<a name="line153"></a>
<a name="line154"></a>  return matches;
<a name="line155"></a>};
</pre>


</body>
</html>
